package com.jianhua.appmarket.common.util;

import com.alibaba.fastjson.JSON;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.jianhua.appmarket.module.entity.system.User;
import com.jianhua.appmarket.module.model.system.UserVO;
import org.joda.time.DateTime;

public class JWTUtil {

    public static final String SECRET = "JIANHUA123.*(#";

    public static String createToken(UserVO user) {
        return JWT.create()
                .withAudience(String.valueOf(user.getId()))
                .withClaim("data", JSON.toJSONString(user))
                //.withIssuedAt(new Date())
                //.withNotBefore(new Date())
                .withExpiresAt(DateTime.now().plusHours(2).toDate())
                .sign(Algorithm.HMAC256(SECRET));
    }

    public static UserVO getUser(String token){
        String userData = JWT.decode(token).getClaim("data").asString();
        return JSON.parseObject(userData,UserVO.class);
    }
}
